package com.vertx.starter;

import com.vertx.starter.mysql.DaoManager;
import com.vertx.starter.mysql.MysqlConfig;
import com.vertx.starter.mysql.MysqlUtil;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;

import java.util.ArrayList;
import java.util.List;

public class Configure {

    private static final Configure ourInstance = new Configure();

    public static Configure getInstance() {
        return ourInstance;
    }

    protected Vertx vertx;

    public MysqlConfig mysqlConfig;

    private MysqlUtil mysqlUtil;

    public DaoManager daoManager;

    public void init(Vertx vertx) {
        this.vertx = vertx;
        initHandler();
        loadConfig();
        initDb();
    }

    private void initDb() {
        List<JsonObject> list = new ArrayList<>();
        for (int i = 0; i < mysqlConfig.getConfigs().size(); i++) {
            list.add(mysqlConfig.getConfigs().getJsonObject(i));
        }
        mysqlUtil = new MysqlUtil(vertx, 2, list);
        daoManager = new DaoManager(mysqlConfig, mysqlUtil);
    }

    private void loadConfig() {
        mysqlConfig = new MysqlConfig(vertx, this.getClass().getResource("/config/mysql.json").getPath());
    }

    private void initHandler() {
    }
}
